<?php

define('BIKE_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
define('BIKE_INCLUDE_DIR', BIKE_ROOT . 'libs' . DIRECTORY_SEPARATOR);
define('BIKE_HTML_ROOT', BIKE_ROOT . 'html' . DIRECTORY_SEPARATOR);
define('BIKE_UPLOAD_DIR', BIKE_HTML_ROOT . 'uploads' . DIRECTORY_SEPARATOR);
define('BIKE_WEB_DIRECTORY_SEPARATOR','/');

require_once(BIKE_ROOT . 'config.php');

header('Content-type: text/html; charset=utf-8');

function bike_autoload($className){
	$fileName = BIKE_INCLUDE_DIR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
	if(file_exists($fileName)){
		require $fileName;
	}else{
		echo '<pre>';
		debug_print_backtrace();
		echo '</pre>';
		die();
	}
}

spl_autoload_register('bike_autoload');

function d($param){
	echo '<pre>';
	var_dump($param);
	echo '</pre>';
}

$configObject = new Bike_Config($globalConfig);

Bike_Service_Locator::addService('Config', $configObject);

$storage = new Bike_Storage_Base(array());

$slaves = $configObject->getValue(array('storage', 'slaves'));
mt_srand();
$serverIndex = mt_rand(0, sizeof($slaves)-1);

$storage->setWritableConnection(Bike_Db::factory($configObject->getValue(array('storage', 'master'))));
$storage->setReadableConnection(Bike_Db::factory($configObject->getValue(array('storage', 'slaves', $serverIndex))));

Bike_Service_Locator::addService('Storage', $storage);
$parserBO = new Bike_Parser_Bike();
$parserBO->setTemplatePath(BIKE_ROOT . 'view' . DIRECTORY_SEPARATOR);

Bike_Service_Locator::addService('Parser_bo', $parserBO);
Bike_Service_Locator::addService('Parser_fe', $parserBO);

Bike_Service_Locator::addService('Request', 'Bike_Request');

$env = new Bike_Environment();
Bike_Service_Locator::addService('Environment',  $env);

Bike_Service_Locator::addService('SiteTree', $env->getEntityStorage('site_tree'));
Bike_Service_Locator::addService('SiteTreeContent', $env->getEntityStorage('site_tree_content'));
Bike_Service_Locator::addService('Users', $env->getEntityStorage('users'));
Bike_Service_Locator::addService('UserManager', 'Bike_Users_User');
Bike_Service_Locator::addService('BO_Session', new Bike_Session('bo'));

$eventsManager = new Bike_Events_EventManager();
//$eventsManager->register('DOCUMENT_NOT_FOUND', 'Custom_Events_DocumentNotFound_News');
//$eventsManager->register('DOCUMENT_NOT_FOUND', 'Custom_Events_DocumentNotFound_Trainings');
Bike_Service_Locator::addService('EventsManager', $eventsManager);